/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2017-7-24
*
\*********************************************************************/

#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
//#include "IicFunc.h"
#include "I2cUserspace.h"
#include "UtilityTH.h"

#define I2C_MASTER_DEV "/dev/i2c-0" //此处使用的IIC在Linux下的设备名称为

int I2csend(unsigned char addr, int len, unsigned char *data)
{
    int fd = open(I2C_MASTER_DEV, O_RDWR);
    if (fd < 0)
    {
        DebugPrintf("i2c open error!\n");
        return -1;
    }
    DebugPrintf("i2c open success!\n");
    ioctl(fd,I2C_TIMEOUT,2);
    ioctl(fd,I2C_RETRIES,1);
    i2c_msg msgs;
    msgs.addr = addr;
    msgs.len = len;
    msgs.flags = 0;
    msgs.buf = data;
    i2c_rdwr_ioctl_data iicData;
    iicData.nmsgs = 1;
    iicData.msgs = &msgs;
    int result = ioctl(fd,I2C_RDWR,&iicData);
    if (result < 0)
    {
        DebugPrintf("i2c send to address %#X error!\n",msgs.addr);
        close(fd);
        return -1;
    }
    DebugPrintf("i2c send to address %#X success!\n",msgs.addr);
    close(fd);
//    usleep(1000*100);
    return 0;
}


void I2ctest()
{
    unsigned char addr = 0x68;
    int len = 3;
    unsigned char data[] = {0x40, 0x08, 0x00};
    I2csend(addr,len,data);
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/



